/**
 * Created by Dalyn on 2017/7/10.
 */
import React from "react";
import Relay from "react-relay";
import {hashHistory} from 'react-router';
import {Icon, Card ,message,Menu, Dropdown,Spin } from 'antd';
import FindProWorkShopMutation from "./mutation/factoryResource/FindProWorkShopMutation"
import FindAllProWorkGroupMutation from "./mutation/factoryResource/FindAllProWorkGroupMutation"
import "./css/productionManagement_common.css";
import "./css/factoryresource.css";
import {Modal,Button,Input} from 'fly';

export default class Factoryresource extends React.Component {
    constructor(props){
        super(props);
        this.state={
            pageSize:"10",
            pageNum:"1",
            totalResult:"",
            proWorkShopList:[],//车间列表
            workShopId:"",//编辑的车间id
            cur_workShopId:"",//当前选中的车间id
            isSys:"",//车间id
            workShopName:"",//车间名
            workShopType:"",//操作类型
            workShopHintText:"",//modal提示文字
            cutWorkGroupList:[],//裁剪班组列表
            tailorWorkGroupList:[],//缝制班组列表
            cleanWorkGroupList:[],//后整班组列表
            packWorkGroupList:[],//包装班组列表
            createTeamType:"",//班组操作类型
            createTeamHintText:"",//班组删除提示
            workGroupId:"",//班组id
            //teamType:"",//班组类型
            //teamName:"",//班组名称
            //workerNum:"",//工人数量
            spinning:true,//加载中
        };
        this.username = sessionStorage.getItem("username");
        this.enterpriseId = sessionStorage.getItem("enterpriseId");
        this.messageFlag = true;
        this.groupType_obj={
            1:"裁剪",
            2:"缝制",
            3:"后整",
            4:"包装",
            }
    }
    componentWillMount(){
        this.findProWorkShop();
    }
    /**
     * 车间*/
    /*查询车间请求*/
    findProWorkShop(workShopId_op){
        Relay.Store.commitUpdate(
            new FindProWorkShopMutation({
                enterpriseId: this.enterpriseId,//企业ID
            }),
            {
                onFailure: () => {
                    message.error('网络请求失败');
                },
                onSuccess: (response) => {
                    let errorCode = response.findProWorkShop.findProWorkShop.errorCode;
                    let value = response.findProWorkShop.findProWorkShop.value;
                    if(errorCode == 0){
                        let proWorkShopList = response.findProWorkShop.findProWorkShop.proWorkShopList;
                        if(workShopId_op){
                            //编辑-添加车间
                            this.findAllProWorkGroup(workShopId_op)
                        }else {
                            //查询车间
                            if(proWorkShopList&&proWorkShopList.length>0){
                                this.setState({
                                    cur_workShopId:proWorkShopList[0].workShopId,
                                    spinning:true,
                                });
                                this.findAllProWorkGroup(proWorkShopList[0].workShopId)
                            }
                        }
                        this.setState({
                            proWorkShopList:proWorkShopList,
                        });
                    }else {
                        message.error(value);
                    }
                }
            }
        )
    }
    /*点击车间*/
    clickWorkShop(cur_workShopId){
        this.setState({
            cur_workShopId:cur_workShopId,
            spinning:true,
        },this.findAllProWorkGroup(cur_workShopId))
    }
    /*车间加载*/
    workShop(){
        let {proWorkShopList,cur_workShopId} = this.state;
        let length = proWorkShopList&&proWorkShopList.length;
        return proWorkShopList&&proWorkShopList.map((v,i)=>{
            let spanClass = (v,i)=>{
                if(v.workShopId == cur_workShopId){//选中状态
                    if(length >= 7){
                        if(i%6==0){//第一个或者第六个
                            return "production_facResource_workshop_span span_background"
                        }else if((i+1)%6 == 0 && i!=(length-1)){
                            return "production_facResource_workshop_spanL production_facResource_workshop_spanR span_background"
                        }else {
                            return "production_facResource_workshop_spanL span_background"
                        }
                    }else {
                        if(i%6==0){//第一个或者第六个
                            return "production_facResource_workshop_span span_background"
                        }else {
                            return "production_facResource_workshop_spanL span_background"
                        }
                    }
                }else {
                    if(length >= 7){
                        if(i%6==0){//第一个或者第六个
                            return "production_facResource_workshop_span"
                        }else if((i+1)%6 == 0 && i!=(length-1)){
                            return "production_facResource_workshop_spanL production_facResource_workshop_spanR"
                        }else {
                            return "production_facResource_workshop_spanL"
                        }
                    }else {
                        if(i%6==0){//第一个或者第六个
                            return "production_facResource_workshop_span"
                        }else {
                            return "production_facResource_workshop_spanL"
                        }
                    }
                }
            };
            if(v.isSys == 1){//默认车间
                return(
                    <span className={spanClass(v,i)} onClick={this.clickWorkShop.bind(this,v.workShopId)}>
                        {v.name}
                    </span>
                )
            }else {
                return(
                    <span className={spanClass(v,i)} onClick={this.clickWorkShop.bind(this,v.workShopId)}>
                        {v.name}
                    </span>
                )
            }
        });

    }
    /**
     * 班组*/
    /*查询班组请求*/
    findAllProWorkGroup(workShopId){
        Relay.Store.commitUpdate(
            new FindAllProWorkGroupMutation({
                workShopId: workShopId,//车间ID
                // enterpriseId: this.enterpriseId,//企业ID
            }),
            {
                onFailure: () => {
                    this.setState({
                        spinning:false,
                    });
                    message.error('网络请求失败');
                },
                onSuccess: (response) => {
                    let errorCode = response.findAllProWorkGroup.findAllProWorkGroup.errorCode;
                    let value = response.findAllProWorkGroup.findAllProWorkGroup.value;
                    if(errorCode == 0){
                        let cutWorkGroupList = response.findAllProWorkGroup.findAllProWorkGroup.cutWorkGroupList;
                        let tailorWorkGroupList = response.findAllProWorkGroup.findAllProWorkGroup.tailorWorkGroupList;
                        let cleanWorkGroupList = response.findAllProWorkGroup.findAllProWorkGroup.cleanWorkGroupList;
                        let packWorkGroupList = response.findAllProWorkGroup.findAllProWorkGroup.packWorkGroupList;
                        this.setState({
                            cutWorkGroupList:cutWorkGroupList,//裁剪班组列表
                            tailorWorkGroupList:tailorWorkGroupList,//缝制班组列表
                            cleanWorkGroupList:cleanWorkGroupList,//后整班组列表
                            packWorkGroupList:packWorkGroupList,//包装班组列表
                            spinning:false,
                        });
                    }else {
                        this.setState({
                            spinning:false,
                        });
                        message.error(value);
                    }
                }
            }
        )
    }
    /*班组卡片点击*/
    clickWorkGroup(v){
        // console.log(v);
        if(v.groupType == 2){
            hashHistory.push({pathname:'/factoryresourceDetailsSewing/',state:{
                workShopId:v.workShopId,
                workGroupId:v.workGroupId,
                workerNum:v.workerNum,
            }});
        }else {
            hashHistory.push({pathname:'/factoryresourceDetailsTailoring/',state:{
                workShopId:v.workShopId,
                workGroupId:v.workGroupId,
                workerNum:v.workerNum,
            }});
        }
        // this.props.location.state.workShopId
    }
    render() {
        let {spinning} = this.state;
        let {cutWorkGroupList,tailorWorkGroupList,cleanWorkGroupList,packWorkGroupList} = this.state;//班组列表
        /*裁剪班组列表*/
        let cutWorkGroup = ()=>{
            return cutWorkGroupList&&cutWorkGroupList.map((v,i)=>{
                let proTask = (v)=>{
                    if(v.proTaskList && v.proTaskList.length>0){
                        return v.proTaskList && v.proTaskList.map((item,index)=>{
                                if(index%4 == 0){
                                    return (
                                        <span className="facResource_card_body_styleNo">{item.styleNo}</span>
                                    )
                                }else {
                                    return (
                                        <span className="facResource_card_body_styleNo facResource_card_left10">{item.styleNo}</span>
                                    )
                                }
                            })
                    }else {
                        return(
                            <span>
                                 <i className="facResource_card_styleNo_empty_img"></i>
                                <span className="facResource_card_styleNo_empty_text">暂无任务</span>
                            </span>

                        )
                    }

                };
                let cardClass = ()=>{
                    if(i%2 == 0){
                        return "production_facResource_team_card card_marginRight"
                    }else {
                        return "production_facResource_team_card"
                    }
                };

                return (
                    <Card
                        title={<span><i className="facResource_card_title_img facResource_cut_title_img"></i>{v.name}</span>}
                        className={cardClass()}
                        style={{ width: "calc((100% - 20px) / 2 - 2px)",}}
                    >
                        <div onClick={this.clickWorkGroup.bind(this,v)}>
                            <p style={{width:"100%",lineHeight: "26px",marginBottom:"5px"}}>
                                <span className="facResource_card_body_pText">工人数量：</span>
                                <span className="facResource_card_body_pText">{v.workerNum}</span>
                                <span className="facResource_card_body_pText"> 人</span>
                            </p>
                            <p style={{width:"100%",lineHeight: "26px"}}>
                                <span className="facResource_card_body_pText">15日生产任务</span>
                            </p>
                            <p className="facResource_card_body_styleNo_box">
                                {
                                    proTask(v)
                                }
                            </p>
                        </div>
                    </Card>
                )
            })
        };
        /*缝制班组列表*/
        let tailorWorkGroup = ()=>{
            return tailorWorkGroupList&&tailorWorkGroupList.map((v,i)=>{
                    let proTask = (v)=>{
                        if(v.proTaskList&&v.proTaskList.length>0){
                            return v.proTaskList && v.proTaskList.map((item,index)=>{
                                    if(index%2 == 0){
                                        return (
                                            <span className="facResource_card_body_styleNo facResource_card_styleNo_tailor">{item.styleNo}</span>
                                        )
                                    }else {
                                        return (
                                            <span className="facResource_card_body_styleNo facResource_card_styleNo_tailor facResource_card_left10">{item.styleNo}</span>
                                        )
                                    }
                                })
                        }else {
                            return(
                                <span className="facResource_card_styleNo_empty_tailor">
                                     <i className="facResource_card_styleNo_empty_img"></i>
                                    <span className="facResource_card_styleNo_empty_text">暂无任务</span>
                                </span>
                            )
                        }
                    };
                    let cardClass = ()=>{
                        if(i%4 == 3){
                            return "production_facResource_team_card_tailor "
                        }else {
                            return "production_facResource_team_card_tailor card_marginRight"
                        }
                    };
                    return (
                        <Card
                            title={<span><i className="facResource_card_title_img facResource_tailor_title_img"></i>{v.name}</span>}
                            className={cardClass()}
                            style={{ width: "calc((100% - 60px) / 4 - 2px)",}}
                        >
                            <div  onClick={this.clickWorkGroup.bind(this,v)}>
                                <p style={{width:"100%",lineHeight: "26px",marginBottom:"5px"}}>
                                    <span className="facResource_card_body_pText">工人数量：</span>
                                    <span className="facResource_card_body_pText">{v.workerNum}</span>
                                    <span className="facResource_card_body_pText"> 人</span>
                                </p>
                                <p style={{width:"100%",lineHeight: "18px"}}>
                                    <span className="facResource_card_body_pText">未来三个月空闲时间：</span>
                                    <span className="facResource_card_body_pText_tailor">{v.freeDays}天</span>
                                </p>
                                <p style={{width:"100%",lineHeight: "26px"}}>
                                    <span className="facResource_card_body_pText">15日生产任务</span>
                                </p>
                                <p className="facResource_card_body_styleNo_box_tailor">
                                    {
                                        proTask(v)
                                    }
                                </p>
                            </div>

                        </Card>
                    )
                })
        };
        /*后整班组列表*/
        let cleanWorkGroup = ()=>{
            return cleanWorkGroupList&&cleanWorkGroupList.map((v,i)=>{
                    let proTask = (v)=>{
                        if(v.proTaskList && v.proTaskList.length>0){
                            return v.proTaskList && v.proTaskList.map((item,index)=>{
                                    if(index%4 == 0){
                                        return (
                                            <span className="facResource_card_body_styleNo">{item.styleNo}</span>
                                        )
                                    }else {
                                        return (
                                            <span className="facResource_card_body_styleNo facResource_card_left10">{item.styleNo}</span>
                                        )
                                    }
                                })
                        }else {
                            return(
                                <span>
                                 <i className="facResource_card_styleNo_empty_img"></i>
                                <span className="facResource_card_styleNo_empty_text">暂无任务</span>
                            </span>

                            )
                        }
                    };
                    let cardClass = ()=>{
                        if(i%2 == 0){
                            return "production_facResource_team_card card_marginRight"
                        }else {
                            return "production_facResource_team_card"
                        }
                    };
                    return (
                        <Card
                            title={<span><i className="facResource_card_title_img facResource_clean_title_img"></i>{v.name}</span>}
                            className={cardClass()}
                            style={{ width: "calc((100% - 20px) / 2 - 2px)",}}
                        >
                            <div onClick={this.clickWorkGroup.bind(this,v)}>
                                <p style={{width:"100%",lineHeight: "26px",marginBottom:"5px"}}>
                                    <span className="facResource_card_body_pText">工人数量：</span>
                                    <span className="facResource_card_body_pText">{v.workerNum}</span>
                                    <span className="facResource_card_body_pText"> 人</span>
                                </p>
                                <p style={{width:"100%",lineHeight: "26px"}}>
                                    <span className="facResource_card_body_pText">15日生产任务</span>
                                </p>
                                <p className="facResource_card_body_styleNo_box">
                                    {
                                        proTask(v)
                                    }
                                </p>
                            </div>

                        </Card>
                    )
                })
        };
        /*包装班组列表*/
        let packWorkGroup = ()=>{
            return packWorkGroupList&&packWorkGroupList.map((v,i)=>{
                    let proTask = (v)=>{
                        if(v.proTaskList && v.proTaskList.length>0){
                            return v.proTaskList && v.proTaskList.map((item,index)=>{
                                    if(index%4 == 0){
                                        return (
                                            <span className="facResource_card_body_styleNo">{item.styleNo}</span>
                                        )
                                    }else {
                                        return (
                                            <span className="facResource_card_body_styleNo facResource_card_left10">{item.styleNo}</span>
                                        )
                                    }
                                })
                        }else {
                            return(
                                <span>
                                     <i className="facResource_card_styleNo_empty_img"></i>
                                    <span className="facResource_card_styleNo_empty_text">暂无任务</span>
                                </span>
                            )
                        }
                    };
                    let cardClass = ()=>{
                        if(i%2 == 0){
                            return "production_facResource_team_card card_marginRight"
                        }else {
                            return "production_facResource_team_card"
                        }
                    };
                    return (
                        <Card
                            title={<span><i className="facResource_card_title_img facResource_pack_title_img"></i>{v.name}</span>}
                            className={cardClass()}
                            style={{ width: "calc((100% - 20px) / 2 - 2px)",}}
                        >
                            <div onClick={this.clickWorkGroup.bind(this,v)}>
                                <p style={{width:"100%",lineHeight: "26px",marginBottom:"5px"}}>
                                    <span className="facResource_card_body_pText">工人数量：</span>
                                    <span className="facResource_card_body_pText">{v.workerNum}</span>
                                    <span className="facResource_card_body_pText"> 人</span>
                                </p>
                                <p style={{width:"100%",lineHeight: "26px"}}>
                                    <span className="facResource_card_body_pText">15日生产任务</span>
                                </p>
                                <p className="facResource_card_body_styleNo_box">
                                    {
                                        proTask(v)
                                    }
                                </p>
                            </div>

                        </Card>
                    )
                })
        };
        // console.log( workShopType);
        return (
            <div className="production_facResource_box">
                <p className="production_facResource_workshop_p">
                    <span className="production_facResource_workshop_spanBox">
                        {this.workShop()}
                    </span>
                </p>
                <div className="production_facResource_spinning_box">
                    {
                        (cutWorkGroupList&&cutWorkGroupList.length>0)
                        ||(tailorWorkGroupList&&tailorWorkGroupList.length>0)
                        ||(cleanWorkGroupList&&cleanWorkGroupList.length>0)
                        ||(packWorkGroupList&&packWorkGroupList.length>0)
                            ?
                            (
                                <div className="production_facResource_team_container">
                                    <div className="production_facResource_team_box">
                                        {/*<p className="production_facResource_team_boxp">裁剪班组</p>*/}
                                        <div className="production_facResource_team_boxdiv">
                                            {
                                                cutWorkGroup()
                                            }
                                        </div>
                                    </div>
                                    <div className="production_facResource_team_box">
                                        {/*<p className="production_facResource_team_boxp">缝制班组</p>*/}
                                        <div className="production_facResource_team_boxdiv">
                                            {
                                                tailorWorkGroup()
                                            }
                                        </div>
                                    </div>
                                    <div className="production_facResource_team_box">
                                        {/*<p className="production_facResource_team_boxp">后整班组</p>*/}
                                        <div className="production_facResource_team_boxdiv">
                                            {
                                                cleanWorkGroup()
                                            }
                                        </div>
                                    </div>
                                    <div className="production_facResource_team_box">
                                        {/*<p className="production_facResource_team_boxp">包装班组</p>*/}
                                        <div className="production_facResource_team_boxdiv">
                                            {
                                                packWorkGroup()
                                            }
                                        </div>
                                    </div>
                                </div>
                            ):(
                            <div className="production_facResource_team_container_empty" style={spinning?{display:"none"}:{display:"block"}}>
                                <i className="facResource_card_team_empty_img"></i>
                                <span className="facResource_card_team_empty_text">暂未创建班组</span>
                            </div>
                        )
                    }
                    <Spin
                        spinning={this.state.spinning}
                        wrapperClassName="production_facResource_spinning_box"
                        className="production_facResource_spinning"
                        size="large"
                        tip="拼命加载中，请稍后！"
                    />
                </div>
            </div>
        )
    }
}
